package se.webbzon.boltzmann.projectile;

import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;

public class Projectile3D {
	
	/** Returns the initial velocity of a projectile that travels from a 
	 * source point to target point with the provided y-acceleration 
	 * and flight time. The duration must at least be 2 steps long. **/
	public static Vector3d getVelocity(Point3d source, Point3d target, double acceleration, int duration) {
		final double t = (double) duration;
		final double h = source.getY() - target.getY();
		final double phi = Math.atan2(	target.getZ() - source.getZ(),
										target.getX() - source.getX());
		final double l = Math.hypot(target.getZ() - source.getZ(), target.getX() - source.getX());
		final double hspeed = l / t;
		final double vspeed = - 0.5 * acceleration * t - h / t;
		
		return new Vector3d(Math.cos(phi) * hspeed, vspeed, Math.sin(phi) * hspeed);
	}

}
